package com.czxy.enterprise.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.TbRecruitmentInfo;
import com.czxy.enterprise.feign.AdvertFeign;
import com.czxy.enterprise.service.RecruitmentInfoService;
import com.czxy.findjob.vo.BaseResult;
import com.czxy.vo.CityVO;
import com.czxy.vo.InfoVo;
import com.czxy.vo.RecruitmentInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Id;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 招聘信息表 前端控制器
 * </p>
 *
 * @author xuewen
 * @since 2022-01-25
 */
@RestController
@RequestMapping("/recruitmentInfo")
public class RecruitmentInfoController {

    @Autowired
    private RecruitmentInfoService recruitmentInfoService;

    @Autowired
    private AdvertFeign advertFeign;

    @PostMapping("/{current}/{size}")
    public BaseResult condition(@PathVariable("current") Integer current, @PathVariable("size") Integer size, @RequestBody RecruitmentInfoVo recruitmentInfoVo){
        Page<TbRecruitmentInfo> page = recruitmentInfoService.condition(current,size,recruitmentInfoVo);
        return BaseResult.ok("查询成功!",page);
    }
    @GetMapping("/{id}")
    public BaseResult<TbRecruitmentInfo> findById(@PathVariable("id") String id){
        TbRecruitmentInfo tbRecruitmentInfo =  recruitmentInfoService.findById(id);
        return BaseResult.ok("查询成功!",tbRecruitmentInfo);
    }
    @GetMapping("/getById/{id}")
    public BaseResult<TbRecruitmentInfo> getById(@PathVariable("id") String id){
        TbRecruitmentInfo tbRecruitmentInfo = null;
        if (StringUtils.isNotBlank(id)){
            recruitmentInfoService.findById(id);
        }
        return BaseResult.ok("查询成功",tbRecruitmentInfo);
    }
    @GetMapping("findByEnterpriseId/{id}")
    public BaseResult findByEnterpriseId(@PathVariable("id") Integer userId){
           List<TbRecruitmentInfo> list =  recruitmentInfoService.findByUserId(userId);
           return BaseResult.ok("查询成功",list);
    }
    @PostMapping("/saveOrUpdate")
    public BaseResult saveOrUpdate(@RequestBody TbRecruitmentInfo tbRecruitmentInfo){
        if (tbRecruitmentInfo.getId()==null){
            tbRecruitmentInfo.setCreateTime(new Date());
        }
        boolean b = recruitmentInfoService.saveOrUpdate(tbRecruitmentInfo);
        advertFeign.delByInfoId(tbRecruitmentInfo.getId());
        if (b){
            return BaseResult.ok("保存成功");
        }else {
            return BaseResult.error("保存失败");
        }
    }
    @DeleteMapping("/delById/{id}")
    public BaseResult delById(@PathVariable("id") Integer id){
        boolean b = recruitmentInfoService.removeById(id);
        advertFeign.delByInfoId(id);
        if (b){
            return BaseResult.ok("删除成功");
        }else {
            return BaseResult.error("删除失败");
        }

    }
    @DeleteMapping("/adminDel/{id}")
    public BaseResult adminDel(@PathVariable("id") Integer Id){
        boolean b = recruitmentInfoService.removeById(Id);
        if (b){
            return BaseResult.ok("删除成功");
        }else {
            return BaseResult.error("删除失败");
        }
    }
    @PostMapping("/condition")
    public BaseResult condition(@RequestBody InfoVo infoVo){
        Page page = recruitmentInfoService.condition(infoVo);
        return BaseResult.ok("查询成功",page);
    }
    @GetMapping("/selectTitle")
    public BaseResult selectTitle(String str){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title",str);
        List<TbRecruitmentInfo> list = recruitmentInfoService.list(queryWrapper);
        return BaseResult.ok("查询成功",list);
    }
    @GetMapping("/findNumByCity")
    public BaseResult<List<CityVO>> findNumByCity() {
        List<CityVO> list = recruitmentInfoService.findNumByCity();
        return BaseResult.ok("查询成功", list);
    }

}

